// pages/live/liveFactory.js
import {Api} from "../../network/api";
import {setNav} from "../../utils/nav";
const fetch = require('../../utils/fetch');
import Wx from "../../common-wx/wx"
import ECache from "../../utils/cache";
import GlobalData from "../../utils/global-data";
import Router from "../../utils/router";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        infoCont: {},
        infoData: [],
        shop_id: '',
        page: 1,
        pageTotal: 2,
        follow: false,
        isSend: false,      // 防止重复请求
        userInfo: {},
        greatCount: 0,      // 粉丝数
        followCount: 0,     // 点赞数
        key: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
		//设置nav
		setNav(this, "工厂详情")
		
        let _this = this;
        if(options.shop_id) {
            this.setData({
                shop_id: options.shop_id
            });
        };
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getInfoCont();

        if(Wx.getStorage(ECache.LIVE_KEY)) {
            this.setData({
                isSend: false,
                page: 1,
                key: Wx.getStorage(ECache.LIVE_KEY)
            });
            this.getData();
        }
        else {
            Router.navTo('Login')
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if(this.data.page > this.data.pageTotal) {
            return false;
        }
        this.getData();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getInfoCont() {
        let _this = this;
        fetch(Api.StoreInfo, {
            method: 'GET',
            data: {
                storeId: _this.data.shop_id
            },
        }).then(res => {
            if (res.status == '0') {
                Wx.showToastError(res.error);
                return false;
            }
            _this.setData({
                infoCont: res.data

            });
            wx.setNavigationBarTitle({
                title: res.data.storeName
            })
        })
    },
    getData() {
        let _this = this;
        if(_this.data.isSend) {
            return false;
        }
        wx.showLoading({
            title: '加载中'
        });
        _this.setData({
            isSend: true
        });
        fetch(Api.LiveHistory, {
            method: 'POST',
            data: {
                key: _this.data.key,
                shop_id: _this.data.shop_id,
                page: _this.data.page
            },
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
        }).then(res => {
            wx.hideLoading();
            _this.setData({
                isSend: false
            });
            if (res.status == '0') {
                Wx.showToastError(res.error);
                return false;
            }
            let arr = _this.data.infoData;
            if(_this.data.page == '1') {
                arr = [];
                _this.setData({
                    pageTotal: res.page_count,
                    shop_data: res.shop_data,
                    follow: res.shop_data.is_follow == 1 ? true : false,
                    followCount: res.shop_data.shop_follow_count,
                    greatCount: res.shop_data.like_count,
                });
            };
            arr = arr.concat(res.list);
            _this.setData({
                infoData: arr,
                page: +_this.data.page + 1
            });
        })
    },
    // 关注工厂
    follow() {
        let _this = this;
        if(_this.data.isSend) {
            return false;
        }
        _this.setData({
            isSend: true
        });
        fetch(Api.LiveFollow, {
            method: 'POST',
            data: {
                key: _this.data.key,
                shop_id: _this.data.shop_id
            },
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
        }).then((res) => {
            _this.setData({
                isSend: false
            });
            Wx.showToastError(res.error)
            if(res.status == '0') {
                return false;
            }
            _this.setData({
                follow: res.has_focus == 1 ? true : false,
                followCount: res.shop_follow_count
            });

        });
    },

    // 点赞
    praise() {
        let _this = this;
        if(_this.data.isSend) {
            return false;
        }
        _this.setData({
            isSend: true
        });
        fetch(Api.LivePraise, {
            method: 'POST',
            data: {
                storeId: _this.data.shop_id,
            },
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
        }).then((res) => {
            _this.setData({
                isSend: false
            });
            if(res.flag > 0) {
                Wx.showToastError('点赞成功');
                _this.setData({
                    greatCount: res.data.greatCount
                });
            }
            else {
                Wx.showToastError(res.data)
            }
        });
    },

    goLive(e) {
        Router.navTo('Live', '?room_id=' + e.currentTarget.dataset.roomid + '&video_url=' + e.currentTarget.dataset.videourl);
    },
})
